/**
 * Created by apple on 16/5/24.
 */
/*
 |--------------------------------------------------------------------------
 | 模块依赖
 |--------------------------------------------------------------------------
 */

import './assets/css/libs.min.css';
import './assets/css/pasp.min.css';
import './assets/sass/main.scss';

import router from './views/router';
import service from './views/service';
import interceptor from './views/interceptor';
import page from './views/pages/index';
import com from './views/components/index';

/*
 |--------------------------------------------------------------------------
 | angular 初始化
 | angular.module('name', [require])
 | 返回 ng 启动时候所需的全局唯一的 app name, 同时声明所需依赖, 加载路由表
 |--------------------------------------------------------------------------
 */

let app = angular
    .module('fe-app', [ 'ui.bootstrap', 'ui.router', 'pasp.ui', 'ngCookies', com.name, page.name ])
    .factory('CommonService', service.factory)
    .factory('Interceptor', interceptor.factory)
    .config($httpProvider => $httpProvider.interceptors.push('Interceptor'))
    .config(router)
    .run(($rootScope, $window, uiAlert) => {
        let $uiAlert;
        $rootScope.$on('unauthorized', () => {
            if (!$uiAlert) {
                $uiAlert = uiAlert.warning('会话已过期, 请重新登录!', {backdrop: 'static', cancelText: false}).then(() => {
                    $window.location.href = '/ctg-mainframe-web'
                })
            }
        })
        $rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
            // 如果是进入登录界面则允许
            if( toState.name =='home' ) {
                $rootScope.isSetPost = true;
            }
        });
        // 项目启动的时候, 初始化需要的东西(如: 事件), 非必须, 可以删除 run 函数
    })
/**
 * 页面加载
 */
let pageModules = require.context('./views/', true, /\.index.js$/);
pageModules.keys().forEach(pageModules);

export default app;